var allpoint=null;
var  cross_type=null;
$(function () {
    $.when( $.get(baseURL + 'eys/epoint/select?pGuid='+ GetUrlParam("pguid"), function (r) {
            allpoint = r.returnlist;
        }),
        getdatadictionary("cross_type")

    ).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'eys/ecrosscost/list?dpGuid='+ GetUrlParam("dpguid")+'&pGuid='+ GetUrlParam("pguid"),
            datatype: "json",
            colModel: [
                { label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true },
                { label: '所属项目ID', name: 'pGuid', index: 'p_guid', width: 80, hidden: true },
                { label: '交叉跨越类型', name: 'cType', index: 'c_type', width: 80 ,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in cross_type) {
                            if (cellvalue == cross_type[d].dKey) return cross_type[d].dValue;
                        }
                    }
                },

                { label: '被交跨物说明', name: 'cCName', index: 'c_c_name', width: 80 },
                { label: '上联点', name: 'cCUpguid', index: 'c_c_upguid', width: 80 ,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in allpoint) {
                            if (cellvalue == allpoint[d].guid) return allpoint[d].pPCode;
                        }
                    }
                },
                { label: '下联点', name: 'cCDownguid', index: 'c_c_downguid', width: 80,
                    formatter: function (cellvalue, options, rowObject) {
                        for (d in allpoint) {
                            if (cellvalue == allpoint[d].guid) return allpoint[d].pPCode;
                        }
                    }
                },
                { label: '跨越杆号', name: 'cCPole', index: 'c_c_pole', width: 80 },
                { label: '跨越档档距（m)', name: 'cCSpan', index: 'c_c_span', width: 80 },
                // { label: '距最近杆号', name: 'cCBestpole', index: 'c_c_bestpole', width: 80 },
                // { label: '距最近杆档距', name: 'cCBestspan', index: 'c_c_bestspan', width: 80 },
                { label: '交叉角度', name: 'cCAngle', index: 'c_c_angle', width: 80 },
                { label: '交叉点净距（m)', name: 'cCCrospan', index: 'c_c_crospan', width: 80 },
                { label: '允许净距（m)', name: 'cCAllowspana', index: 'c_c_allowspana', width: 80 },
                { label: '导线横截面积(mm2)', name: 'cCArea', index: 'c_c_area', width: 80 },
                { label: '被跨越物宽度(m)', name: 'cCWidth', index: 'c_c_width', width: 80 },
                // { label: '单价', name: 'cCPrice', index: 'c_c_price', width: 80 },
                // { label: '定位方式', name: 'cCAutotype', index: 'c_c_autotype', width: 80 },
                { label: '人工经度', name: 'cCInputlongitude', index: 'c_c_inputlongitude', width: 80 },
                { label: '人工纬度', name: 'cCInputlatitude', index: 'c_c_inputlatitude', width: 80 },
                { label: '采集时间', name: 'cCCollecttime', index: 'c_c_collecttime', width: 80,
                    formatter:function(value,options,row){
                        return value!=null?new Date(value).Format('yyyy-MM-dd'):"";}
                }

            ],
            viewrecords: true,
            height: 385,
            rowNum: 10,
            rowList : [10,30,50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth:true,
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader : {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames : {
                page:"page",
                rows:"limit",
                order: "order"
            },
            gridComplete:function(){
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            }
        })
    )
});
function getdatadictionary(sss)
{

    $.get(baseURL + "api/bdatadictionary/select?d_code="+sss, function(r){
        if(sss=="cross_type")
        {
            cross_type=r.returnlist;
        }

    });
}
var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        eCrossCost: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.eCrossCost = {};
        },
        update: function (event) {
            var guid = getSelectedRow();
            if(guid == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.eCrossCost.guid == null ? "eys/ecrosscost/save" : "eys/ecrosscost/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.eCrossCost),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if(guids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "eys/ecrosscost/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(guid){
            $.get(baseURL + "eys/ecrosscost/info/"+guid, function(r){
                vm.eCrossCost = r.eCrossCost;
                vm.eCrossCost.cCCollecttime=  r.eCrossCost.cCCollecttime !=null?new Date( r.eCrossCost.cCCollecttime ).Format('yyyy-MM-dd'):"";

            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page
            }).trigger("reloadGrid");
        }
    }
});